{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "# os_train=pd.read_csv(r'D:\\Data\\TCForNewComer\\deal\\Same_userid\\shop\\shop_train.csv')\n",
    "# test=pd.read_csv(r'D:\\Data\\TCForNewComer\\deal\\Same_userid\\shop\\shop_test.csv')\n",
    "off_train=pd.read_csv(r'D:\\Data\\TCForNewComer\\new_script1\\train_off_train.csv')\n",
    "test=pd.read_csv(r'D:\\Data\\TCForNewComer\\new_script1\\train_test.csv')\n",
    "ftest=pd.read_csv(r'D:\\Data\\TCForNewComer\\new_script1\\train_ftest.csv')\n",
    "ftesty=pd.read_csv(r'D:\\Data\\TCForNewComer\\new_script1\\ftesty.csv')\n",
    "# off_train_same_umid=pd.read_csv(r'D:\\Data\\TCForNewComer\\new_script1\\sameumid\\off_train_same_umid.csv')\n",
    "# sampling=pd.read_csv(r'D:\\Data\\TCForNewComer\\new_script1\\sameumid\\sampling.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cor=sampling.corr()\n",
    "cor[abs(cor)<0.1]=0\n",
    "cor[cor['Date']==0].index\n",
    "# cor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "len(sampling)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ftest.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# train=os_train[['User_id', 'Merchant_id','Discount_rate','Distance','Date_received','Date']].copy()\n",
    "os_train=os_train[['User_id', 'Coupon_id','Merchant_id','Discount_rate','Distance', \n",
    "       'Date_received', 'Date']].copy()\n",
    "train=os_train.copy()\n",
    "xtrain=train.loc[train[train['Date_received']<600].index]\n",
    "X=xtrain.drop(['Date'],axis=1)\n",
    "y=xtrain['Date']\n",
    "xtest=train.loc[train[train['Date_received']>=600].index]\n",
    "test=xtest.drop(['Date'],axis=1)\n",
    "testa=xtest['Date']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train=os_train.copy()\n",
    "os_train=os_train[['User_id', 'Merchant_id', 'Coupon_id', 'Discount_rate', 'Distance',\\\n",
    "       'Date_received', 'Date', 'ord_buy','Avg_distance','same_Coupon_count']].copy()\n",
    "test=test[['User_id', 'Merchant_id', 'Coupon_id', 'Discount_rate', 'Distance',\\\n",
    "       'Date_received','ord_buy','Avg_distance','same_Coupon_count']].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "os_train['Date_received']=os_train['Date_received'].apply(lambda x: int(str(x)[1:])).copy()\n",
    "test['Date_received']=test['Date_received'].apply(lambda x: int(str(x)[1:])).copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "off_train_same_umid.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "off_train_same_umid.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "off_train_same_umid.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "combine=[off_train_same_umid,ftest,test]#,sampling]\n",
    "for data in combine:\n",
    "    data['Date_received']=data['Date_received'].astype('category')\n",
    "    new_dummies=pd.get_dummies(data['Date_received'],prefix='Date_received')\n",
    "    data=pd.concat([data,new_dummies],axis=1)\n",
    "    data.drop(['Date_received'],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import xgboost as xgb\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import auc\n",
    "from sklearn.metrics import roc_auc_score,auc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for h in np.arange(46000,60000,1000):\n",
    "    posi=off_train[off_train['Date']==1]\n",
    "    negt=off_train[off_train['Date']==0]\n",
    "    temp=negt.sample(h,random_state=1000)\n",
    "    train=pd.concat([posi,temp],axis=0)\n",
    "\n",
    "    X=train[train['month']<6].copy()\n",
    "    y=X['Date']\n",
    "    X=X.drop(['Date','month'],axis=1)#['Date','month','User_id']\n",
    "    Test=ftest.drop(['Date'],axis=1).copy()\n",
    "    ftesty['Coupon_id']=Test['Coupon_id']\n",
    "\n",
    "    #全部  0.5552 learning_rate=0.09\n",
    "    model=xgb.XGBClassifier(n_estimators=200,\n",
    "                            max_depth=2,#6\n",
    "                            objective=\"binary:logistic\",\n",
    "                            learning_rate=0.09, \n",
    "                            subsample=.8,\n",
    "                            min_child_weight=6,\n",
    "                            colsample_bytree=.4,\n",
    "                            scale_pos_weight=1.6,\n",
    "                            gamma=9,\n",
    "                            seed=100,\n",
    "                            reg_alpha=8,\n",
    "                            reg_lambda=1.3)\n",
    "    print('sampling: ',h)\n",
    "    model.fit(X,y)\n",
    "    pred=model.predict_proba(X)[:,1]\n",
    "    print('accuracy:',model.score(X,y))\n",
    "    pred_test=model.predict_proba(Test)[:,1]\n",
    "\n",
    "    predict_test=model.predict(Test)\n",
    "    print(' test accuracy:',accuracy_score(ftesty['Date'],predict_test))\n",
    "    print('roc_auc_score:',roc_auc_score(ftesty['Date'],pred_test))\n",
    "    print('Avg_auc for full test set:',Avg_auc(ftesty,pred_test))\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "posi=off_train[off_train['Date']==1]\n",
    "negt=off_train[off_train['Date']==0]\n",
    "temp=negt.sample(57000,random_state=1000)\n",
    "train=pd.concat([posi,temp],axis=0)\n",
    "train.index=np.arange(0,len(train),1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "K=6\n",
    "from sklearn.metrics import roc_auc_score,auc\n",
    "from time import sleep\n",
    "import winsound\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "# kf = KFold(n_splits = K,random_state = 1,shuffle = True)\n",
    "# kf=StratifiedKFold(n_splits = K,random_state = 90,shuffle = True)\n",
    "# X=train[train['month']<6].copy()\n",
    "# y=X['Date']\n",
    "# X=X.drop(['Date','month'],axis=1)#['Date','month','User_id']\n",
    "# Test=ftest.drop(['Date'],axis=1).copy()\n",
    "# ftesty['Coupon_id']=Test['Coupon_id']\n",
    "\n",
    "\n",
    "# X=sampling.drop(['Date','User_id', 'Discount_rate', 'Distance', 'Date_received',\n",
    "#        'Coupon_month_num', 'month', 'discount_rate', 'Courate_month_m6',\n",
    "#        'Coupon_month_getm30', 'Coupon_premonth_get'],axis=1).copy()\n",
    "# test=test.drop(['User_id', 'Discount_rate', 'Distance', 'Date_received',\n",
    "#        'Coupon_month_num',  'discount_rate', 'Courate_month_m6',\n",
    "#        'Coupon_month_getm30', 'Coupon_premonth_get'],axis=1).copy()\n",
    "# y=sampling['Date']\n",
    "\n",
    "X=train.drop(['Date','month'],axis=1).copy()\n",
    "test=test.copy()\n",
    "y=train['Date']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/4, random_state=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def Avg_auc(data,pred):\n",
    "    aucc=0\n",
    "    i=0\n",
    "    df=data.copy()\n",
    "    df['y_valid_pred']=pred\n",
    "    for j in list(set(df['Coupon_id'].values)):\n",
    "        df_1=df[df['Coupon_id']==j]\n",
    "        if len(np.unique(df_1['Date']))==1:\n",
    "            continue\n",
    "        aucc=aucc+roc_auc_score(df_1['Date'],df_1['y_valid_pred'])\n",
    "        i+=1\n",
    "    aucc=aucc/i                                                                      #len(set(df['Coupon_id'].values))\n",
    "    df.drop(['y_valid_pred'],axis=1,inplace=True)\n",
    "    return aucc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model=DecisionTreeClassifier(max_depth=6,min_samples_split=10,min_samples_leaf=6,max_features=5,random_state=100)\n",
    "model.fit(X,y)\n",
    "pred=model.predict_proba(ftest)[:,1]\n",
    "print('accuracy:',model.score(ftest,ftesty['Date']))\n",
    "# pred_test=model.predict_proba(ftest)[:,1]\n",
    "print('Avg_auc for full training set:',Avg_auc(ftesty,pred))\n",
    "\n",
    "importances = model.feature_importances_\n",
    "indices=np.argsort(-importances)\n",
    "feat_names=X.columns\n",
    "print(importances[indices])\n",
    "print(X.columns[indices])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "combine=[X]\n",
    "def Discount_rate(x):#转化函数\n",
    "    if x.startswith('0'):\n",
    "        return float(x)\n",
    "    else:\n",
    "        return float(x.split(':')[0])-float(x.split(':')[1])\n",
    "for data in combine:\n",
    "    data.index=np.arange(0,len(data),1)\n",
    "    data['Discount_rate']=data['Discount_rate'].apply(Discount_rate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 121395 entries, 0 to 121394\n",
      "Data columns (total 10 columns):\n",
      "User_id          121395 non-null int64\n",
      "Merchant_id      121395 non-null int64\n",
      "Coupon_id        121395 non-null int64\n",
      "Discount_rate    121395 non-null float64\n",
      "Distance         121395 non-null float64\n",
      "Date_received    121395 non-null int64\n",
      "discount_rate    121395 non-null float64\n",
      "ord_buy          121395 non-null int64\n",
      "same_merc        121395 non-null int64\n",
      "same_dis         121395 non-null int64\n",
      "dtypes: float64(3), int64(7)\n",
      "memory usage: 10.2 MB\n"
     ]
    }
   ],
   "source": [
    "X.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set(test['same_dis'].values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuracy: 0.797792330821\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "DataFrame.dtypes for data must be int, float or bool.\nDid not expect the data types in fields Discount_rate, Distance",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-e5eb02c0a41e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[0mpred\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredict_proba\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     19\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'accuracy:'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscore\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[0mpred_test\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredict_proba\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtest\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     21\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     22\u001b[0m \u001b[1;31m# predict_test=model.predict(Test)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\xgboost\\sklearn.py\u001b[0m in \u001b[0;36mpredict_proba\u001b[1;34m(self, data, output_margin, ntree_limit)\u001b[0m\n\u001b[0;32m    472\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    473\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mpredict_proba\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moutput_margin\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mntree_limit\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 474\u001b[1;33m         \u001b[0mtest_dmatrix\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mDMatrix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmissing\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmissing\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    475\u001b[0m         class_probs = self.booster().predict(test_dmatrix,\n\u001b[0;32m    476\u001b[0m                                              \u001b[0moutput_margin\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0moutput_margin\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\xgboost\\core.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, data, label, missing, weight, silent, feature_names, feature_types)\u001b[0m\n\u001b[0;32m    253\u001b[0m         data, feature_names, feature_types = _maybe_pandas_data(data,\n\u001b[0;32m    254\u001b[0m                                                                 \u001b[0mfeature_names\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 255\u001b[1;33m                                                                 feature_types)\n\u001b[0m\u001b[0;32m    256\u001b[0m         \u001b[0mlabel\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_maybe_pandas_label\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    257\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\xgboost\\core.py\u001b[0m in \u001b[0;36m_maybe_pandas_data\u001b[1;34m(data, feature_names, feature_types)\u001b[0m\n\u001b[0;32m    179\u001b[0m         msg = \"\"\"DataFrame.dtypes for data must be int, float or bool.\n\u001b[0;32m    180\u001b[0m Did not expect the data types in fields \"\"\"\n\u001b[1;32m--> 181\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmsg\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m', '\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbad_fields\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    182\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    183\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mfeature_names\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: DataFrame.dtypes for data must be int, float or bool.\nDid not expect the data types in fields Discount_rate, Distance"
     ]
    }
   ],
   "source": [
    "import xgboost as xgb\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import auc\n",
    "#全部  0.5552 learning_rate=0.09\n",
    "model=xgb.XGBClassifier(n_estimators=200,\n",
    "                        max_depth=2,#6\n",
    "                        objective=\"binary:logistic\",\n",
    "                        learning_rate=0.09, \n",
    "                        subsample=.8,\n",
    "                        min_child_weight=6,\n",
    "                        colsample_bytree=.4,\n",
    "                        scale_pos_weight=1.6,\n",
    "                        gamma=9,\n",
    "                        seed=100,\n",
    "                        reg_alpha=8,\n",
    "                        reg_lambda=1.3)\n",
    "model.fit(X,y)\n",
    "pred=model.predict_proba(X)[:,1]\n",
    "print('accuracy:',model.score(X,y))\n",
    "pred_test=model.predict_proba(test)[:,1]\n",
    "\n",
    "# predict_test=model.predict(Test)\n",
    "# print(' test accuracy:',accuracy_score(ftesty['Date'],predict_test))\n",
    "# print('roc_auc_score:',roc_auc_score(ftesty['Date'],pred_test))\n",
    "# print('Avg_auc for full test set:',Avg_auc(ftesty,pred_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测1的准确率是： 0.5166641068960222\n",
      "预测0的准确率是： 0.9222670930693994\n"
     ]
    }
   ],
   "source": [
    "ftesty['predict_test']=predict_test\n",
    "a1=ftesty[ftesty['Date']==1]\n",
    "a0=ftesty[ftesty['Date']==0]\n",
    "print('预测1的准确率是：',len(a1[a1['predict_test']==1])/len(a1))\n",
    "print('预测0的准确率是：',len(a0[a0['predict_test']==0])/len(a0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ftesty.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sampling[sampling['User_id']==947]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "off_train_same_umid[off_train_same_umid['User_id']==947]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model=GradientBoostingClassifier(learning_rate=0.01,n_estimators=200,max_depth=7,min_samples_split=3,max_features=3)#0.89\n",
    "# model.fit(X,y)\n",
    "# pred=model.predict_proba(X)[:,1]\n",
    "# print('accuracy:',model.score(test,testa))\n",
    "# pred_test=model.predict_proba(test)[:,1]\n",
    "# print('Avg_auc for full training set:',Avg_auc(os_train[os_train['Date_received']>=600],testa))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model=RandomForestClassifier(n_estimators=200,max_depth=7,min_samples_split=3,max_features=3)\n",
    "# model.fit(X,y)\n",
    "# pred=model.predict_proba(X)[:,1]\n",
    "# print('accuracy:',model.score(test,testa))\n",
    "# pred_test=model.predict_proba(test)[:,1]\n",
    "# print('Avg_auc for full training set:',Avg_auc(os_train[os_train['Date_received']>=600],testa))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Flod: 0 \n",
      "Flod: 1 \n",
      "Flod: 2 \n",
      "Flod: 3 \n",
      "Flod: 4 \n",
      "Flod: 5 \n",
      "Flod: 6 \n",
      "Flod: 7 \n",
      "Flod: 8 \n",
      "Flod: 9 "
     ]
    }
   ],
   "source": [
    "y_test_pred=0\n",
    "y_valid_pred=0*y\n",
    "fit_model=[]\n",
    "# XMIN=pd.DataFrame(X_test_std_scaled,columns=['Merchant_id', 'Coupon_id', 'Discount_rate', 'Distance', 'Date_received'])\n",
    "for i,(train_index,test_index) in enumerate(kf.split(X,y)):\n",
    "    y_train,y_valid=y.iloc[train_index],y.iloc[test_index]\n",
    "    x_train,x_valid=X.iloc[train_index],X.iloc[test_index]\n",
    "    print('\\nFlod:',i,end=' ')\n",
    "    fitted_model=model.fit(x_train,y_train)\n",
    "    pred= fitted_model.predict_proba(x_valid)[:,1]\n",
    "    y_valid_pred.iloc[test_index]=pred\n",
    "#     print(pd.DataFrame(pred).head(1))\n",
    "    \n",
    "    y_test_pred+=fitted_model.predict_proba(test)[:,1]\n",
    "    fit_model.append(fitted_model)\n",
    "#     winsound.Beep(600,1000)\n",
    "    sleep(1)\n",
    "y_test_pred/=K\n",
    "# print('Avg_auc for full training set:',Avg_auc(train,y_valid_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# y_test_pred=0\n",
    "# y_valid_pred=0*y\n",
    "# fit_model=[]\n",
    "# # XMIN=pd.DataFrame(X_test_std_scaled,columns=['Merchant_id', 'Coupon_id', 'Discount_rate', 'Distance', 'Date_received'])\n",
    "# for i,(train_index,test_index) in enumerate(kf.split(X,y)):\n",
    "#     y_train,y_valid=y.iloc[train_index],y.iloc[test_index]\n",
    "#     x_train,x_valid=X.iloc[train_index],X.iloc[test_index]\n",
    "#     print('\\nFlod:',i,end=' ')\n",
    "#     fitted_model=model.fit(x_train,y_train)\n",
    "#     pred= fitted_model.predict_proba(x_valid)[:,1]\n",
    "#     y_valid_pred.iloc[test_index]=pred\n",
    "# #     print(pd.DataFrame(pred).head(1))\n",
    "    \n",
    "#     y_test_pred+=fitted_model.predict_proba(Test)[:,1]\n",
    "#     fit_model.append(fitted_model)\n",
    "# #     winsound.Beep(600,1000)\n",
    "#     sleep(1)\n",
    "# y_test_pred/=K\n",
    "# # print('Avg_auc for full training set:',Avg_auc(train,y_valid_pred))\n",
    "# print('Avg_auc for full test set:',Avg_auc(ftesty,y_test_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "xgb=y_test_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "gbdt=y_test_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rf=y_test_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "gbdt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "h=pd.DataFrame()\n",
    "h['xgb']=xgb\n",
    "h['rf']=rf\n",
    "h['adb']=gbdt\n",
    "h['avg']=h.mean(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "h.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "tt=pd.read_csv(r'D:\\Data\\TCForNewComer\\ccf_offline_stage1_test_revised.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sub=pd.DataFrame()\n",
    "sub['User_id']=tt['User_id']\n",
    "sub['Coupon_id']=tt['Coupon_id']\n",
    "sub['Date_received']=tt['Date_received']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sub['Probability']=pred_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sub.to_csv(r'D:\\Data\\TCForNewComer\\new_script1\\xgbpred_result.csv',float_format='%.1f',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
